package cn.tedu.knows.sys.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


// 要写@Component保存到Spring容器中
@Component
public class DemoInterceptor implements
                            HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 控制器方法运行之前运行
        // 返回值表示是否运行继续访问控制器
        // 返回true表示放行,才能运行控制器方法内容
        System.out.println("preHandle运行");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 控制器方法运行之后运行
        System.out.println("postHandle运行");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 视图处理器运行后运行(异步请求没有这个步骤)
        System.out.println("afterCompletion运行");
    }
}
